package itsm.isperp.module.controller.task;

import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilter;
import itsm.isperp.framework.web.request.JqGridFilterRule;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.configuration.ConfCategory;
import itsm.isperp.module.entity.task.TskInspectCiCateCheck;
import itsm.isperp.module.service.configuration.ConfCategoryService;
import itsm.isperp.module.service.task.TskInspectCiCateCheckService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2015-05-09 22:45
 */
@Controller
@RequestMapping("task/inspectCiCateCheck")
public class TskInspectCiCateCheckController extends
		BaseController<TskInspectCiCateCheck> {

	@Autowired
	protected TskInspectCiCateCheckService tskInspectCiCateCheckService;

	@Autowired
	ConfCategoryService confCategoryService;

	@Override
	public TskInspectCiCateCheckService getService() {
		return this.tskInspectCiCateCheckService;
	}

	@Override
	public String getPageTitle() {
		return "巡检任务";
	}

	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {

		ConfCategory category = null;
		ModelAndView mv = new ModelAndView(this.getModulePath() + "/edit");
		TskInspectCiCateCheck model = null;
		if (StringUtils.isEmpty(id)) {
			String categoryId = req.getParameter("categoryId");
			if (StringUtils.isEmpty(categoryId)) {
				return null;
			}
			category = confCategoryService.findOne(categoryId);
			model = new TskInspectCiCateCheck();
			model.setConfCategoryId(categoryId);
		} else {
			model = tskInspectCiCateCheckService.findOne(id);
			category = confCategoryService.findOne(model.getConfCategoryId());
		}
		mv.addObject("pageTitle", this.getPageTitle());
		mv.addObject("model", model);
		mv.addObject("categoryName", category.getName());
		return mv;
	}

	/**
	 * 根据配置分类Id查询所包含的所有自定义属性
	 * 
	 * @param categoryId
	 *            配置分类Id
	 * @return json数据结果
	 */
	@RequestMapping("listByCiCategory")
	@ResponseBody
	public List<Map<String, Object>> listByCategory(String categoryId,
			JqGridRequest req) {

		if (StringUtils.isEmpty(categoryId)) {
			JqGridFilter filter = req.getFilter();
			if (filter == null) {
				return null;
			}
			List<JqGridFilterRule> list = filter.getRules();
			for (JqGridFilterRule rule : list) {
				if ("categoryId".equals(rule.getField())) {
					categoryId = rule.getData();
					break;
				}
			}
		}
		if (StringUtils.isEmpty(categoryId)) {
			return null;
		}
		List<Map<String, Object>> list = this.getService().findAllByCiCategory(
				categoryId);
		return list;
	}

}
